﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FATCASchema.Nodes
{
    public class AccountReportNode : iNode
    {
        public string Name { get; private set; }
        public bool AllowAddChildren { get; private set; }
        public List<iNode> Children { get; private set; }
        public object NodeObject { get; private set; }

        public AccountReportNode(CorrectableAccountReport_Type nodeObject)
        {
            this.Name = GetName(nodeObject);
            this.NodeObject = nodeObject;
            this.AllowAddChildren = false;
            this.LoadChildren();
        }

        private string GetName(CorrectableAccountReport_Type nodeObject)
        {
            string name = "Account Report";

            if (nodeObject == null || nodeObject.AccountHolder == null || nodeObject.AccountHolder.Items == null|| nodeObject.AccountHolder.Items.Count() == 0)
                return name;


            foreach (var accountHolder in nodeObject.AccountHolder.Items)
            {
                if (accountHolder is PersonParty_Type)
                {
                    if (((PersonParty_Type)accountHolder).Name != null)
                    {
                        foreach (var personName in ((PersonParty_Type)accountHolder).Name)
                        {
                            return name + " - " + personName.FirstName.Value + " " + personName.LastName.Value;
                        }
                    }
                }
                else if (accountHolder is OrganisationParty_Type)
                {
                    if (((OrganisationParty_Type)accountHolder).Name != null)
                    {
                        foreach (var orgName in ((OrganisationParty_Type)accountHolder).Name)
                        {
                            return name + " - " + orgName.Value;
                        }
                    }

                    return name;
                }
            }

            return name;
        }

        public void LoadChildren()
        {
            if (this.NodeObject == null)
                return;

            //var nodeObject = (Fatca_TypeReportingGroup)this.NodeObject;
            //this.Children = new List<iNode>();
            
            //if (nodeObject.Sponsor != null)
            //    this.Children.Add(new CorrectableOrganisationParty("Sponsor", nodeObject.Sponsor));

            //if (nodeObject.Intermediary != null)
            //    this.Children.Add(new CorrectableOrganisationParty("Intermediary", nodeObject.Intermediary));

            //foreach (var item in nodeObject.ReportingGroup)
            //{
            //    this.Children.Add(new ReportingGroupNode(item));
            //}
        }

        public void AddChild()
        { }

        public void LoadPane()
        { }
    }
}
